﻿using System.Collections;                                  
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class player : MonoBehaviour
{
    //起跳速度
    protected float j_speed = 8.0f;
    //跳跃次数
    private int jump = 2;
    //起跳高度
    protected int j_height=2;
    //跳跃速度
    float mspeed = 3f;
    //存活
    public bool alive;
    gamemanager gm;
    protected bool cd=true;
    public float cdtime = 15f;
    //bool isLanding = true;
    // Update is called once per frame
    //死亡
    void dead() {
        alive = false;
        Time.timeScale = 1;
        Debug.Log("youdied");
        //死亡跳转在gm
        Destroy(this.gameObject);
    }
    private void Start()
    {
        
        gm = GameObject.FindGameObjectWithTag("gm").GetComponent<gamemanager>();
        alive = true;
    }
    void Update()
    {
        if (gm.score <= 1000)
        {
            DingWei();
        }
        else {
            GuoGuan();
        }

        //跳
        if (jump > 0 &&/* Input.GetMouseButtonDown(0)*/ Input.GetKeyDown(KeyCode.A) && gm.ting == false)
        {
            jump--;
            Jump();
        }
        
        //降
        if (Input.GetKeyDown(KeyCode.S) && gm.ting == false) {
            this.GetComponent<Rigidbody>().velocity = Vector3.down*2 * this.j_speed;
        }
        if (cd == true&&Input.GetKey(KeyCode.A)) {
            TheWorld();
        }
    }
    //复位
    void DingWei() {
        
        float x = transform.position.x;
        float y = transform.position.y;
        float z = transform.position.z;
        this.transform.Translate(mspeed * Time.deltaTime, 0, 0, Space.World);
        if (x <= -8||y<=-0.5) {
            dead();
        }
        if (x > -2)
        {
            transform.position = new Vector3(-2f, y, z);
        }
        
    }
    //达成目标
    void GuoGuan() { 
        transform.Translate(mspeed *5* Time.unscaledDeltaTime, 0, 0, Space.World);
        Invoke(nameof(ZhuanChang),3);
    }
    //达成目标转场
    void ZhuanChang() {
        SceneManager.LoadScene(2);
    }
    //跳跃
    void Jump()
        {
            this.GetComponent<Rigidbody>().velocity = Vector3.up * this.j_speed;
            //this.transform.Translate(0, j_height, 0, Space.World);
        }
    //技能
    void TheWorld() {
        Time.timeScale = 0.5f;
        Invoke(nameof(RtTime), 1f);
        cd = false;
    }
    //技能结束
    void RtTime() {
        Time.timeScale = 1f;
        Cdtime();
    }
    //技能冷却
    void Cdtime() {
        if (!cd) {
            float todo = cdtime;
            todo -= Time.deltaTime;
            if (todo <= 0) {
                cd = true;
            }
        }
    }
      
    void OnCollisionEnter(Collision collision)
    {//蓝块碰撞检测  
        if (collision.transform.tag == "floor")
        {
            jump = 2;
        }//黑块碰撞检测
        else if (collision.transform.tag == "kill")
        {
            dead();
        }
        
        //isLanding = true;
        
    }
    
    private void OnCollisionStay(Collision collision)
    {//黄块碰撞检测
        if (collision.transform.tag == "candy")
        {
            jump = 2;
            Jump();
        }
        if(collision.transform.tag == "build") 
        {
            jump = 2;

        }
    }
}


